package com.rokyinfo.controller.v1;

import com.rokyinfo.controller.BasicPath;
import com.rokyinfo.domain.SysUser;
import com.rokyinfo.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yuanzhijian on 2017/1/8.
 */
@Api(value = "/users" ,tags = "SysUser" ,description = "系统用户")
@RestController
@RequestMapping(value = BasicPath.V1 + "/users")
public class SysUserController {

    @Autowired
    private SysUserService mSysUserService;

    @ApiOperation(value = "获取系统用户列表", notes = "")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity<List<SysUser>> getUserList() throws Exception {
        // 处理"/users/"的GET请求，用来获取用户列表
        // 还可以通过@RequestParam从页面中传递参数来进行查询条件或者翻页信息的传递

        return new ResponseEntity<List<SysUser>>(new ArrayList<SysUser>(), HttpStatus.OK);
    }

    @ApiOperation(value = "创建用户", notes = "根据SysUser对象创建用户")
    @ApiImplicitParam(name = "sysUser", value = "用户详细实体sysUser", required = true, dataType = "SysUser")
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public ResponseEntity postUser(@ModelAttribute SysUser sysUser) {
        // 处理"/users/"的POST请求，用来创建User
        // 除了@ModelAttribute绑定参数之外，还可以通过@RequestParam从页面中传递参数
        URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(mSysUserService.createSysUser(sysUser)).toUri();
        return ResponseEntity.created(location).build();
    }


    @ApiOperation(value = "获取系统用户详细信息", notes = "根据url的id来获取用户详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResponseEntity<SysUser> getUser(@PathVariable Long id) {
        // 处理"/users/{id}"的GET请求，用来获取url中id值的User信息
        // url中的id可通过@PathVariable绑定到函数的参数中
        return ResponseEntity.ok(mSysUserService.getSysUserByName("admin"));
    }

    @ApiOperation(value = "更新系统用户详细信息", notes = "根据url的id来指定更新对象，并根据传过来的sysUser信息来更新系统用户详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "sysUser", value = "用户详细实体sysUser", required = true, dataType = "SysUser")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public ResponseEntity putUser(@PathVariable Long id, @ModelAttribute SysUser sysUser) {
        // 处理"/users/{id}"的PUT请求，用来更新User信息

        return ResponseEntity.ok().build();
    }


    @ApiOperation(value = "删除系统用户", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseEntity deleteUser(@PathVariable Long id) {
        // 处理"/users/{id}"的DELETE请求，用来删除User
        return ResponseEntity.ok().build();
    }

}
